import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-l","--lpath",help="ROUTE 文件路径")
args = parser.parse_args()

if args.lpath:
    route_path = args.lpath
else:
    parser.print_help()
    exit()

with open (route_path, 'r' ) as f:
    lines = f.readlines()

x = []
y = []
mode1 = []
mode2 = []
index = []

for line in lines:
    part = line.strip().split()
    x.append(float(part[1]))
    y.append(float(part[2]))
    mode1.append(int(part[3]))
    mode2.append(int(part[4]))
    index.append(int(part[5]))

def plot_mode1():
    plt.figure()
    colors1 = ['r', 'g', 'b', 'y', 'c', 'm']
    unique_mode1 = list(set(mode1))
    last_index_value = 1
    for i in range(len(unique_mode1)):
        x_values = [x[j] for j in range(len(x)) if mode1[j] == unique_mode1[i]]
        y_values = [y[j] for j in range(len(y)) if mode1[j] == unique_mode1[i]]
        color = colors1[i % len(colors1)]
        plt.scatter(x_values, y_values, s=10, color=color, label=f'Mode1 {unique_mode1[i]}')

    plt.xlabel('X')
    plt.ylabel('Y')
    plt.title('X-Y (Mode 1)')
    plt.legend()

def plot_mode2():
    plt.figure()
    colors2 = ['k', 'r', 'g', 'b', 'y', 'c', 'm']
    unique_mode2 = list(set(mode2))
    for i in range(len(unique_mode2)):
        x_values = [x[j] for j in range(len(x)) if mode2[j] == unique_mode2[i]]
        y_values = [y[j] for j in range(len(y)) if mode2[j] == unique_mode2[i]]
        color = colors2[i % len(colors2)]
        plt.scatter(x_values, y_values, s=10, color=color, label=f'Mode2 {unique_mode2[i]}')

def animate(i):
    plt.clf()
    plt.plot(x[:i+1], y[:i+1], '-o', color='b')
    if i > 0:
        plt.plot(x[i-1:i+1], y[i-1:i+1], '-o', color='r')
    # plt.plot(x[:i+1], y[:i+1], '-o')
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.axis('equal')
    plt.title(f'X-Y (Line) Frame {i+1}')

# plot_mode1()
# plot_mode2()

ani = FuncAnimation(plt.gcf(), animate, frames=len(x), interval=1, repeat=False)

plt.show()

